package com.china.springServer.utils;

import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.message.BasicNameValuePair;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @fileName: HttpUtils
 * @author: LmR
 * @createDate: 2023/10/26 14:03
 * @Desc:
 */
public class HttpUtils {

    /**
     * Http四要素：
     * 请求行
     * 请求头
     * 空行
     * 请求体：body或form
     *
     * @param url
     * @return
     */

    public static String doGet(String url, Map<String, String> param) throws IOException, URISyntaxException {
        if (url == null || "".equals(url)) {
            return null;
        }
        CloseableHttpClient httpClient = null;
        CloseableHttpResponse httpResponse = null;
        try {
            // 创建HttpClient对象
            httpClient = HttpClientBuilder.create().build();

            // 创建uri,完成参数拼接
            URIBuilder builder = new URIBuilder(url);
            if (param != null) {
                for (String key : param.keySet()) {
                    builder.addParameter(key, param.get(key));
                }
            }
            URI uri = builder.build();

            // 创建 http Get请求
            HttpGet httpGet = new HttpGet(uri);

            // 执行http GET 请求
            httpResponse = httpClient.execute(httpGet);
            if (200 != httpResponse.getStatusLine().getStatusCode()) {
                return "error";
            }
            HttpEntity entity = httpResponse.getEntity();
            return entity.toString();
        } catch (Exception e) {
            return e.toString();
        } finally {
            try {
                if (httpResponse != null) {
                    httpResponse.close();
                }
                httpClient.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }


    public static String doPost(String url, Map<String, String> params) {
        if (url == null || "".equals(url))
            return null;
        CloseableHttpClient httpClient = HttpClientBuilder.create().build();

        CloseableHttpResponse response = null;
        String result = "";
        try {
            // 创建Http Post请求
            HttpPost httpPost = new HttpPost(url);
            httpPost.addHeader("Content-Type", "application/json; charset=utf-8");
            httpPost.addHeader("X-Api-Sign-Version", "2.0.0");
            if (params != null) {
                List<NameValuePair> paramList = new ArrayList<>();
                for (String key : params.keySet()) {
                    paramList.add(new BasicNameValuePair(key, params.get(key)));
                }
                UrlEncodedFormEntity entity = new UrlEncodedFormEntity(paramList);
                httpPost.setEntity(entity);
            }
            response = httpClient.execute(httpPost);
            if (200 != response.getStatusLine().getStatusCode()) {
                result = "error";
            }
            result = response.getEntity().toString();
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return e.toString();
        } finally {
            try {
                if (response != null) {
                    response.close();
                }
                httpClient.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
